# ======================================================================
#
# Copyright (C) ADN
# ADN::LOG::Log Log.pm
# Description:
#  - Gestion des logs de la sonde
#
# ======================================================================

package ADN::LOG::Log;

use strict;
use ADN::XML::OBJtoXML;
use ADN::CONF::Read;
use File::Path;

# #######################
# Création de l'objet self + init. variables
# #######################
sub new
{
	# Construction de l'objet $self
	my $self = shift;
	my $class = ref($self) || $self;
	return $self if ref $self;
	$self = bless {} => $class;
 	if ( defined $self->_init() and ( $self->_init()) =~ m/^[-]1$/sxm ) {
   		return -1;
 	}
 	return $self;
}

# #######################
# Fonction d'initialisation
# #######################
sub _init
{
	my $self = shift;
  	# Chargement du fichier de configuration XML + Initialisation variable.
  	my $conf = ADN::CONF::Read->new();
  	if ( $conf =~ m/^[-]1$/sxm ) {
    		return -1;
  	}
  	my %log_conf = $conf->log_conf();
	$self->{quotas} = $log_conf{quotas};
	$self->{file_log} = $log_conf{file_log};
	$self->{file_xml} = $log_conf{file_xml};
	undef $conf;
}

# #######################
# Deuxieme partie de la fonction principale
# #######################
sub log_2
{
    my $self = shift;
    my( $Message, $level, $option ) = (shift, shift, shift);

    if ( $option == 1 || $option == 3 || $option == 5 ) {
	    $self->open_file($self->{file_log});
      push @{$self->{tab}}, '-- [' . localtime() . "] - [$level]\n$Message\n"; 
  		$self->quotas();
      $self->save( $self->{file_log} );
    }
    if ( $option == 4 || $option == 5 ) {
      	print STDOUT "$Message\n";
    }
    $Message = $level = $option = undef;
    return;
}

# #######################
# Fonction principale du module
# #######################
sub Log
{
  	my $self = shift;
  	my( $Message, $level, $option ) = (shift, shift, shift);

    if ( $^O eq 'MSWin32' ) {
		  mkpath( $ENV{CommonProgramFiles} . '/ADN/');
		  $self->{DB_DIR} = $ENV{CommonProgramFiles} . '/ADN/'; 
  	}
	  elsif ( $^O eq 'linux' ) {
		  mkpath( '/tmp/ADN' );
		  $self->{DB_DIR} = '/tmp/ADN';
	  }
  	if ( defined $Message && defined $level && defined $option ) {
    		if ( ( $Message =~ m/^4500$/smx ) && ( $level =~ m/^4500$/smx ) && ( $option =~ m/^4500$/smx ) ) {
      			return $self->aff_server( $level );
    		}
    		else {
     			$self->log_2($Message, $level, $option);
    		}
  	}
    $Message = $level = $option = undef;
}

# #######################
# Fonction Lecture d'un fichier
# #######################
sub open_file
{
  	my $self = shift;
  	my $name_file = shift;

  	@{$self->{tab}} = ();
  	if ( -e "$self->{DB_DIR}/$name_file" && -f "$self->{DB_DIR}/$name_file" && -r "$self->{DB_DIR}/$name_file" ) {
    		if( open LOG, '<', "$self->{DB_DIR}/$name_file" ) {
      			while ( my $l1 = <LOG> ) {
        			push @{$self->{tab}}, $l1; 
      			}
      			if (!close LOG) {}
    		}
  	}
  	undef $name_file;
  	return;
}

# #######################
# Fonction Lecture d'un fichier + renvoie
# => Serveur
# #######################
sub aff_server
{
  	my $self = shift;
  	my $option = shift;

  	@{$self->{tab}} = ();
  	if ( $option == 4500 ) {
    		if ( -e "$self->{DB_DIR}/$self->{file_log}" && -f "$self->{DB_DIR}/$self->{file_log}" && -r "$self->{DB_DIR}/$self->{file_log}") {
      			if( open LOG, '<', "$self->{DB_DIR}/$self->{file_log}" ) {
        			while (my $l1 = <LOG>) {
          				push @{$self->{tab}}, $l1;
        			}
        			undef $option;
        			if (!close LOG) {}
        			return (\@{$self->{tab}});
      			}
    		}
  	}
}

# #######################
# Fonction sauvegarde dans un fichier
# #######################
sub save
{
	my $self = shift;
	my $name_file = shift;

  if( open LOG, '>', "$self->{DB_DIR}/$name_file" ) { 
	  foreach my $l ( @{$self->{tab}} ) {
  		if ( defined($l) ) {
     				print LOG $l;   
   		} else {
	          $name_file = $l = undef;
     				if (!close LOG) {}
     				return;
    	}
    }
    if (!close LOG) {}
  }
  undef $name_file;
  return;
}

# #######################
# Fonction quotas
# #######################
sub quotas
{
	my $self = shift;
	my $cnt = 0;
	my @tab;

 	@{$self->{tab}} = reverse @{$self->{tab}};
	foreach my $l ( @{$self->{tab}} ) {
  	if ( $cnt <= $self->{quotas} ) {
			$tab[$cnt] = $self->{tab}[$cnt];
  	}
		$cnt++;
	}
	if ($cnt > 0) {
		@{$self->{tab}} = reverse @tab;
	} else {
		@{$self->{tab}} = reverse @{$self->{tab}};
	}
	undef $cnt;	
	return;
}

# #######################
# Fonction effacer un fichier
# #######################
sub delete
{
	my $self = shift;
	my $name_file = shift;

	if ( $name_file =~ m/^log$/ismx ) {
    		$name_file = $self->{name_txt};
  	}
  	elsif ( $name_file =~ m/^xml$/ismx ) {
    		$name_file = $self->{name_xml};
  	}
  	else {
    		return 0;
  	}
  	unlink "$self->{DB_DIR}/$name_file";
  	undef $name_file;
  	return;
}

# #######################
# DESTROY OBJECT
# #######################
sub DESTROY {
	my $self = shift;
	return;
}
1;
__END__
